home *** CD-ROM | disk | FTP | other *** search
/ PC Player 2004 May / pc player 2004-05.iso / Demos / FarCry / Data1.cab / _1734C975B8AD4B329F1CF51BD38FB25D < prev    next >
Encoding:
Text File  |  2004-01-06  |  1.7 KB  |  85 lines

  1. ----------------------------------------------------------------------------
  2. CI_NEWTABLE=1
  3. CI_ENDTABLE=2
  4. CI_STRING=3
  5. CI_NUMBER=4
  6.  
  7.  
  8. function WriteIndex(stm,idx)
  9.     if(type(idx)=="number")then
  10.         stm:WriteBool(1);
  11.         stm:WriteInt(idx);
  12.     elseif(type(idx)=="string")then
  13.         stm:WriteBool(nil);
  14.         stm:WriteString(idx);
  15.     else
  16.         System:Log("Unrecognized idx type");
  17.     end
  18. end
  19.  
  20. function ReadIndex(stm)
  21.     local bNumber=stm:ReadBool();
  22.     if(bNumber)then
  23.         return stm:ReadInt();
  24.     else
  25.         return stm:ReadString();
  26.     end
  27. end
  28.  
  29. function WriteToStream(stm,t,name)
  30.     if(name==nil)then
  31.         name="__root__"
  32.     end
  33.     if(type(t)=="table")then
  34.         stm:WriteByte(CI_NEWTABLE);
  35.         WriteIndex(stm,name);
  36.         for i,val in t do
  37.             WriteToStream(stm,val,i);
  38.         end
  39.         stm:WriteByte(CI_ENDTABLE);
  40.     else
  41.         local tt=type(t);
  42.         if(tt=="string")then
  43.             stm:WriteByte(CI_STRING);
  44.             WriteIndex(stm,name);
  45.             stm:WriteString(t);
  46.             --System:Log("STRING "..name.." "..t);
  47.         elseif(tt=="number")then
  48.             stm:WriteByte(CI_NUMBER);
  49.             WriteIndex(stm,name);
  50.             stm:WriteFloat(t);
  51.             --System:Log("NUMBER "..name.." "..t);
  52.         end
  53.     end
  54. end
  55.  
  56. function ReadFromStream(stm,parent)
  57.     local chunkid=stm:ReadByte();
  58.     local idx=nil;
  59.     local val;
  60.     if(chunkid~=CI_ENDTABLE)then
  61.         idx=ReadIndex(stm);
  62.         if(chunkid==CI_NEWTABLE)then
  63.         ------------------------------
  64.             val={}
  65.             while(ReadFromStream(stm,val)~=CI_ENDTABLE)do end
  66.             if(parent)then
  67.                 parent[idx]=val;
  68.             end
  69.         ------------------------------
  70.         elseif (chunkid==CI_STRING)then
  71.             parent[idx]=stm:ReadString();
  72.         elseif (chunkid==CI_NUMBER)then
  73.             parent[idx]=stm:ReadFloat();
  74.         end
  75.     end
  76.  
  77.     if(parent==nil)then
  78.         return val;
  79.     else
  80.         return chunkid;
  81.     end
  82. end
  83.  
  84. --WriteToStream(ProximityTrigger);
  85. --table=ReadFromStream(stm)